package org.wells.dlock;

import java.util.HashMap;
import java.util.Map;

public final class DLockBuilder {

	private static final Map<String, DLock> zkDistributedLockMap = new HashMap<>();
	private String zkAddress;

	private static volatile DLockBuilder instance;

	private DLockBuilder(String zkAddress) {
		super();
		this.zkAddress = zkAddress;
	}

	public static DLockBuilder getInstance(String zkAddress) {// 单例

		if (instance == null) {
			synchronized (DLockBuilder.class) {
				if (instance == null) {
					instance = new DLockBuilder(zkAddress);
				}
			}
		}

		return instance;
	}

	/**
	 * 锁实现
	 * 
	 * @param lockName 锁name
	 * @return
	 */
	public DLock getLock(String lockName) {
		return zkDistributedLockMap.computeIfAbsent(lockName, k -> new DLock(zkAddress, lockName));
	}

}
